<template>
  <div class="my-container">
    <!-- 个人中心：已登录页头 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image
            class="avatar"
            width="100"
            height="100"
            fit="cover"
            round
            :src="userInfo.photo"
          />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button class="right-btn" round size="mini">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 个人中心：未登录页头 -->
    <div class="header not-login" v-else>
      <div class="login-btn" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" class="mobile-img" />
        <span>登陆 / 注册</span>
      </div>
    </div>

    <!-- 宫格导航 -->
    <div>
      <van-grid class="grid-nav" :column-num="2" clickable>
        <van-grid-item class="grid-nav-item">
          <i slot="icon" class="toutiao toutiao-shoucang"></i>
          <span slot="text" class="text">收藏</span>
        </van-grid-item>
        <van-grid-item class="grid-nav-item">
          <i slot="icon" class="toutiao toutiao-lishi"></i>
          <span slot="text" class="text">历史</span>
        </van-grid-item>
      </van-grid>
    </div>

    <!-- 单元格 -->
    <van-cell class="cell-item-msg" title="消息通知" is-link />
    <van-cell class="cell-item-xz" title="小智同学" is-link />
    <!-- 退出登录 -->
    <van-button
      class="not-login-btn"
      type="white"
      block
      v-if="user"
      @click="logOff"
      >退出登录</van-button
    >
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'

export default {
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    // 如果已登录，调用loadUserInfo，开始加载用户信息
    if (this.user) {
      this.loadUserInfo()
    }
  },
  mounted () {},
  methods: {
    // 加载登录用户信息
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        // console.log(data);
        this.userInfo = data.data
      } catch (error) {
        this.$toast('获取数据失败，请稍后重试')
      }
    },
    // 退出登录
    logOff () {
      // 组件中需要使用this.$dialog调用弹出框
      this.$dialog
        .confirm({
          title: '确认退出登录吗？'
        })
        .then(() => {
          /*
           * 确定退出，清除Vuex和本地存储的登录状态
           * 因为将登录信息存入Vuex后，又将Vuex内的用户信息存入本地中
           * 所以只需要把Vuex中的登录信息设置为null即可
           * commit: 向Vuex提交数据
           */
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // 取消不做操作
        })
    }
  }
}
</script>

<style lang="less" scoped>
.my-container {
  // 头部通用背景样式
  .header {
    height: 361px;
    //样式中可使用~@代替../选择路径
    background: url("~@/assets/banner.png");
    background-size: cover;
  }
  //  未登录样式
  .not-login {
    // 弹性布局
    display: flex;
    // 主轴方向水平居中
    justify-content: center;
    // 主轴方向垂直居中
    align-items: center;
    .login-btn {
      display: flex;
      // 设置主轴方向是垂直方向
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      span {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  // 已登录样式
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      display: flex;
      // 怪异盒模型，重新计算盒子宽高（包括内外边距）
      box-sizing: border-box;
      // 沿主轴水平方向两端对齐
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        element.style {
          width: 70px;
          height: 70px;
        }
        .avatar {
          width: 170px !important;
          height: 170px !important;
          margin-right: 23px;
          border: 4px solid #fff;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
      .right {
        .right-btn {
          color: #666;
        }
      }
    }
    .data-stats {
      height: 130px;
      display: flex;
      // justify-content: space-between;
      // align-items: center;
      //flex （flex属性是flex-grow, flex-shrink 和 flex-basis的简写，默认值为0 1 auto。后两个属性可选）
      //                   放大，       缩小，项目占据主轴的大小
      .data-item {
        height: 130px;
        flex: 1; //
        display: flex;
        flex-direction: column; //设置主轴为垂直方向，默认row水平方向
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  // 宫格导航样式
  .grid-nav {
    .grid-nav-item {
      height: 141px;
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .cell-item-msg {
    margin-top: 10px;
  }
  .cell-item-xz {
    margin-bottom: 10px;
  }
  .not-login-btn {
    background-color: #fff;
    color: #666;
    border: none;
  }
}
</style>
